package member;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class SigninAjax extends DispatchAction {
	/**
	 * ID중복검사를 위한 메소드
	 * */
	public ActionForward validateMid(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		String result = "";
		
		String mid = request.getParameter("mid");
		MemberDAO memberDAO = new MemberDAO();
		
		int r = memberDAO.getMemberByLetter(mid);
		if(r!=0){
			result = "invalid";
		} else result = "ok";
//		System.out.println(result);
		PrintWriter out = response.getWriter();
		out.print(result);
		out.close();
		return null;
	}

	public ActionForward getMids(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String mname = URLDecoder.decode(request.getParameter("fname"), "UTF-8");	//한글 깨짐 방지를 위한 디코딩
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		String result = "";
		
		MemberDAO memberDAO = new MemberDAO();
		
		ArrayList mnames = memberDAO.getMembersByMname(mname);
		
		result += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		result += "<mnames>";
		for(int i=0;i<mnames.size();i++){
			String name = (String)mnames.get(i);
			result +="<mname>"+name+"</mname>";
		}
		result +="</mnames>";
		
		PrintWriter out = response.getWriter();
		out.print(result);
		
		out.close();
		
		return null;
	}
	
	public ActionForward getMpass(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("utf-8");
		
		String id = URLDecoder.decode(request.getParameter("fid"), "UTF-8");	//한글 깨짐 방지를 위한 디코딩
		String email = URLDecoder.decode(request.getParameter("femail"), "UTF-8");	//한글 깨짐 방지를 위한 디코딩
				
		String result = "";
		
		MemberDAO memberDAO = new MemberDAO();
		
		String mpass = memberDAO.getMembersByMemail(id, email);
		
		result += "<?xml version=\"1.0\" encoding=\"euc-kr\"?>";
		
		result += "<data>";
		
		if(mpass!=null) result +="<mpass>"+mpass+"</mpass>";
		
		result +="</data>";
		
		
		PrintWriter out = response.getWriter();
		System.out.println(result);
		System.out.println("id : "+id);
		System.out.println("email : "+email);
		out.print(result);
		
		out.close();
		
		return null;
	}
	
	
	
}
